package com.servlet;

import com.pojo.Button;
import com.pojo.Resources;
import com.pojo.User;
import com.service.ResourcesService;
import com.service.impl.ResourcesServiceImpl;
import com.util.BaseServlet;
import com.util.IninJson;
import com.util.LayuiTable;
import com.util.ResultData;
import com.vo.Menus;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@WebServlet("/menu")
public class MenuServlet extends BaseServlet {
    ResourcesService resourcesService = new ResourcesServiceImpl();

    public String getMenuBtn(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession(true);
        User user = (User)session.getAttribute("user");
        int uid=user.getId();
        int mid = Integer.parseInt(request.getParameter("mid"));
        List<Resources> resourcesList = resourcesService.findAll(uid,mid);
        session.setAttribute("resourceBtn",resourcesList);
        return "system/menu/menuList";
    }

    public LayuiTable<Menus> getResource(HttpServletRequest request, HttpServletResponse response){
        LayuiTable<Menus> layuiTable=resourcesService.allResouce();
        return layuiTable;
    }

    public List<Button> allButtenType(HttpServletRequest request, HttpServletResponse response){
        List<Button> list=resourcesService.getFindButtonAll();
        return list;
    }

    public List<Resources> menuByType(HttpServletRequest request, HttpServletResponse response){
        int type = Integer.parseInt(request.getParameter("mtype"));
        List<Resources> allResources = resourcesService.findAllResources(type);
        return allResources;
    }

    public ResultData allMenuById(HttpServletRequest request, HttpServletResponse response){
        int menuid = Integer.parseInt(request.getParameter("menuid"));
        Resources allResources = resourcesService.findResourcesById(menuid);
        ResultData<Resources> resultData = new ResultData<Resources>();
        resultData.setData(allResources);
        return resultData;
    }

    public ResultData addResources(HttpServletRequest request, HttpServletResponse response){
        String mname = request.getParameter("mname");
        String resUrl = request.getParameter("resUrl");
        String mfunction = request.getParameter("mfunction");
        int type2 = Integer.parseInt(request.getParameter("type2"));
        String icon = request.getParameter("icon");
        String mbtn = request.getParameter("mbtn");
        int father = Integer.parseInt(request.getParameter("father"));

        Resources resources = new Resources();
        resources.setResName(mname);
        if(type2==2){
            resources.setResUrl(resUrl);
        }else if(type2==3){
            resources.setResUrl(mbtn);
        }
        resources.setResKey(mfunction);
        resources.setIcon(icon);
        resources.setType(type2-1);
        resources.setParendId(father);

        ResultData<User> resultData = new ResultData<User>();
        int i = resourcesService.addServiceResources(resources);
        if(i>0){
            resultData.setMsg("权限添加成功");
            resultData.setStatus(200);
        }
        return resultData;
    }

    public IninJson selelctMenu(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession(true);
        User user = (User)session.getAttribute("user");
        int uid=user.getId();
        IninJson ininJson=resourcesService.findMenuAll(uid);
        return ininJson;
    }





    @Override
    public Class<?> getServletClass() {
        return MenuServlet.class;
    }
}
